package client

import (
	"github.com/smallnest/rpcx/client"
	"github.com/smallnest/rpcx/protocol"
	"sync"
	"time"
	"wzgames/internal/gloader"
	"wzgames/internal/services"
)

// betower rpc client

var (
	betowerPool *client.XClientPool
	betowerOnce sync.Once
)

func InitBetTowerClient() {
	// 详见RPCx文档
	option := client.DefaultOption
	option.ConnectTimeout = 5 * time.Second
	option.Heartbeat = true
	option.HeartbeatInterval = 15 * time.Second
	option.Retries = 1
	option.SerializeType = protocol.ProtoBuffer
	d := gloader.NewServiceDiscovery(services.RPCxServiceBetTower)
	betowerPool = client.NewXClientPool(3, services.RPCxServiceBetTower, client.Failover, client.RoundRobin, d, option)
}

func GetBetTowerClient() client.XClient {
	if betowerPool == nil {
		betowerOnce.Do(func() {
			InitBetTowerClient()
		})
	}
	return betowerPool.Get()
}

func CloseBetTowerClient() {
	if betowerPool != nil {
		betowerPool.Close()
	}
}
